package com.icim.xmtm098.export.service;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import javax.annotation.Resource;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import avic.bh.ws.common.BhException_Exception;
import avic.bh.ws.common.ClientInfo;
import avic.bh.ws.common.ComWsService;
import avic.bh.ws.common.IComWs;
import avic.bh.ws.common.IqcEaInfo;
import avic.bh.ws.common.IqcEaInfoId;
import avic.bh.ws.common.OqcEaInfo;
import avic.bh.ws.common.OqcEaInfoId;
import avic.bh.ws.common.OqcInfo;
import avic.bh.ws.common.OqcInfoId;
import avic.bh.ws.common.Rmsg;

import com.icim.csot.exporter.tx.csotshipping.CsotShippingO;
import com.icim.dao.RetPrdInfoDao;
import com.icim.entity.Ret_prd_info;
import com.icim.util.TimeUtil;

@Service
public class XmtmExportShippingData098Service implements IXmtmExportShippingData098Service {

	@Resource
	private RetPrdInfoDao retPrdInfoDao;

	private Logger logger = LoggerFactory.getLogger(XmtmExportShippingData098Service.class);

	@Override
	public CsotShippingO sendShippingData(String userName,String key,String model,String boxId,String prdQty) throws Exception {
		CsotShippingO outTrx = new CsotShippingO();
		
		Timestamp currentTimestamp = TimeUtil.getCurrentTimestamp();

	    IComWs iComWs = new ComWsService().getComWsPort();
	    ClientInfo clientInfo = new ClientInfo();
	    clientInfo.setSystemId("WG");
	    clientInfo.setUserName(userName);
	    clientInfo.setUserPwd(key);
	    rtnDataFor098Detail(iComWs, clientInfo, currentTimestamp,model,boxId,prdQty);

		outTrx.setRtn_code("0000000");
		outTrx.setRtn_mesg("SUCCESS");
		return outTrx;
	}

	private void rtnDataFor098Detail(IComWs iComWs, ClientInfo clientInfo, Timestamp gEvtTimestamp,String model,String boxId,String prdQty) throws DatatypeConfigurationException, BhException_Exception {
      //oqcinfo
      logger.info("098客户回传OQC_INFO数据开始，boxId："+boxId);
      OqcInfo oqcInfo = new OqcInfo();
      OqcInfoId oqcInfoId = new OqcInfoId();
      oqcInfoId.setBoxId(boxId);
      //AB减镀品 、B品是镀膜品 、A减镀品
      oqcInfo.setModel(model);
      oqcInfo.setId(oqcInfoId);
      oqcInfo.setWorkQty(new BigDecimal(prdQty));
      GregorianCalendar gc = new GregorianCalendar();
      gc.setTime(new Date(gEvtTimestamp.getTime()));
      XMLGregorianCalendar cal = DatatypeFactory.newInstance().newXMLGregorianCalendar(gc);
      oqcInfo.setStkinDate(cal);
      Rmsg msg=iComWs.createOrUpdateOQCINFO(clientInfo,oqcInfo);
      logger.info("098客户回传OQC_INFO数据结果："+msg.getRmsg());
      
      List<Ret_prd_info> infoList = retPrdInfoDao.getSheetList(boxId);
      if(infoList!=null){
        List<OqcEaInfo> oqcEaInfoList = new ArrayList<OqcEaInfo>();
        List<IqcEaInfo> iqcEaInfoList = new ArrayList<IqcEaInfo>();
        for (Ret_prd_info info : infoList) {
          //oqc_ea_info
//          logger.info("098客户回传OQC_EA_INFO数据开始，sheetId："+info.getPrd_seq_id());
          OqcEaInfo oqcEaInfo = new OqcEaInfo();
          OqcEaInfoId oqcEaInfoId = new OqcEaInfoId();
          oqcEaInfoId.setBoxId(boxId);
          oqcEaInfo.setId(oqcEaInfoId);
          oqcEaInfoId.setEaId(info.getPrd_seq_id());
//          Rmsg oqcEaMsg = iComWs.createOrUpdateOQCEAINFO(clientInfo, oqcEaInfo);
          oqcEaInfoList.add(oqcEaInfo);
//          logger.info("098客户回传OQC_EA_INFO数据结果："+oqcEaMsg.getRmsg());
          
          // iqc_ea_info
//          logger.info("098客户回传IQC_EA_INFO数据开始，sheetId："+info.getPrd_seq_id());
          IqcEaInfo iqcEaInfo = new IqcEaInfo();
          IqcEaInfoId iqcEaInfoId = new IqcEaInfoId();
          iqcEaInfoId.setEaId(info.getPrd_seq_id());
          iqcEaInfo.setId(iqcEaInfoId);
          //AB减镀品 、B品是镀膜品 、A减镀品
          iqcEaInfo.setModel(model);
          gc.setTime(info.getFst_logon_timestamp());
          XMLGregorianCalendar cal1 = DatatypeFactory.newInstance().newXMLGregorianCalendar(gc);
          iqcEaInfo.setInputDate(cal1);
//          Rmsg iqcEaMsg = iComWs.createOrUpdateIQCEAINFO(clientInfo, iqcEaInfo);
          iqcEaInfoList.add(iqcEaInfo);
//          logger.info("098客户回传IQC_EA_INFO数据结果："+iqcEaMsg.getRmsg());
        }
        Rmsg oqcEaMsg = iComWs.createOrUpdateOQCEAINFOLIST(clientInfo, oqcEaInfoList);
        logger.info("098客户回传OQC_EA_INFO数据结果："+oqcEaMsg.getRmsg());
        Rmsg iqcEaMsg = iComWs.createOrUpdateIQCEAINFOLIST(clientInfo, iqcEaInfoList);
        logger.info("098客户回传IQC_EA_INFO数据结果："+iqcEaMsg.getRmsg());
      }
    }
	
	
	
	public static void main(String[] args) throws DatatypeConfigurationException, BhException_Exception {
      IComWs iComWs = new ComWsService().getComWsPort();
      ClientInfo clientInfo = new ClientInfo();
      clientInfo.setSystemId("WG");
      clientInfo.setUserName("wgbh");
      clientInfo.setUserPwd("wgbhdata");
      
      //oqcinfo
      OqcInfo oqcInfo = new OqcInfo();
      OqcInfoId oqcInfoId = new OqcInfoId();
//      oqcInfoId.setBoxId("WZH098170619001050");
      oqcInfoId.setBoxId("WZH0981706190010");
      oqcInfo.setId(oqcInfoId);
//      oqcInfo.setModel("098TJB01/TDM01");
      oqcInfo.setModel("098TJB01/T");
      oqcInfo.setWorkQty(new BigDecimal(2));
      GregorianCalendar gcal =new GregorianCalendar();
      XMLGregorianCalendar xgcal= DatatypeFactory.newInstance().newXMLGregorianCalendar(gcal);
      oqcInfo.setStkinDate(xgcal);
      Rmsg msg=iComWs.createOrUpdateOQCINFO(clientInfo,oqcInfo);
      System.out.println(msg.getRmsg());
      
      List<OqcInfo> dataList = iComWs.queryOQCINFO(clientInfo);
      System.out.println("oqcInfo.size:"+dataList.size());
      
      //oqc_ea_info
      OqcEaInfo oqcEaInfo = new OqcEaInfo();
      OqcEaInfoId oqcEaInfoId = new OqcEaInfoId();
      oqcEaInfoId.setBoxId("TESTEAID0000001");
      oqcEaInfoId.setEaId("EAID0001");
      oqcEaInfo.setId(oqcEaInfoId);
      List<OqcEaInfo> oqcEaInfoList = new ArrayList<OqcEaInfo>();
      oqcEaInfoList.add(oqcEaInfo);
      Rmsg oqcEaMsg = iComWs.createOrUpdateOQCEAINFOLIST(clientInfo, oqcEaInfoList);
      System.out.println(oqcEaMsg.getRmsg());
      List<OqcEaInfo> oqcEaInfoList1 = iComWs.queryOQCEAINFO(clientInfo);
      System.out.println("oqcEaInfo.size:"+oqcEaInfoList1.size());
      
      // iqc_ea_info
      IqcEaInfo iqcEaInfo = new IqcEaInfo();
      IqcEaInfoId iqcEaInfoId = new IqcEaInfoId();
      iqcEaInfoId.setEaId("WGEAID1");
      iqcEaInfo.setId(iqcEaInfoId);
      iqcEaInfo.setModel("WGMODEL");
      iqcEaInfo.setInputDate(xgcal);
      List<IqcEaInfo> iqcEaInfoList = new ArrayList<IqcEaInfo>();
      iqcEaInfoList.add(iqcEaInfo);
      Rmsg iqcEaMsg = iComWs.createOrUpdateIQCEAINFOLIST(clientInfo, iqcEaInfoList);
      System.out.println(iqcEaMsg.getRmsg());
      List<IqcEaInfo> iqcEaInfoList1 = iComWs.queryIQCEAINFO(clientInfo);
      System.out.println("iqcEaInfo.size:"+iqcEaInfoList1.size());
    }
}
